﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Sripirom.Pattern.UnitTest.Core.UnitTest
{
                                                         
    //The name is typically the same as the abstract class, minus the Provider part. Sticking to our (fake) example. we'd have a static class called Data.
    public static class UnitTestRepository<T> where T : ConfigurationSection
    {
        private static bool _isInitialized = false;

        private static T _config = null;
        public static T Load
        {                             
            get
            {
                Initialize();
                return (T)_config;
            }
        }

        public static T Select(String categoryName, String className)
        {

            Initialize();
            return (T)_config;
            
        }

        //private static ConfigProviderCollection _providers;
        //public static ConfigProviderCollection Providers
        //{
        //    get
        //    {
        //        Initialize();
        //        return _providers;
        //    }
        //}
        private static void Initialize(String categoryName, String className)
        {
            T dataConfig = null;

            if (!_isInitialized)
            {
                // get the configuration section for the feature
                dataConfig = (T)ConfigurationManager.GetSection("UnitTestCategoryRepository");

                if (dataConfig == null)
                {
                    throw new ConfigurationErrorsException("Data is not configured to be used with this application");
                }
                _config = dataConfig;
                

                _isInitialized = true;
            }
        }
        private static void Initialize()
        {
            T dataConfig = null;

            if (!_isInitialized)
            {
                // get the configuration section for the feature
                dataConfig = (T)ConfigurationManager.GetSection("UnitTestCategoryRepository");

                if (dataConfig == null)
                {
                    throw new ConfigurationErrorsException("Data is not configured to be used with this application");
                }
                _config = dataConfig;
               // _providers = new ConfigProviderCollection();

                // use the ProvidersHelper class to call Initialize() on each provider
                //ProvidersHelper.InstantiateProviders(dataConfig.Providers, _providers, typeof(BaseConfigProvider));

                // set a reference to the default provider
               // _config = _providers[dataConfig.DefaultProvider] as BaseConfigProvider;

                _isInitialized = true;
            }
        }

        //public static void Get()
        //{
        //    Initialize();
        //    if (_config != null)
        //    {
        //        _config.Get();
        //    }
        //}

        //public static void Delete()
        //{
        //    Initialize();
        //    if (_config != null)
        //    {
        //        _config.Delete();
        //    }
        //}


    }



}
